package com.zhouzi.zhaoyang.ui.activity

import android.content.Intent
import android.os.Build
import android.text.InputType
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.afollestad.materialdialogs.MaterialDialog
import com.zhouzi.zhaoyang.R
import com.zhouzi.zhaoyang.base.App
import com.zhouzi.zhaoyang.base.BaseActivity
import com.zhouzi.zhaoyang.bean.GetOrderListBean2
import com.zhouzi.zhaoyang.bean.OrderDetailBean
import com.zhouzi.zhaoyang.mvp.p.TransactionQueryDetailActivityPresenter
import com.zhouzi.zhaoyang.mvp.v.ITransationQueryDetailActivityView
import com.zhouzi.zhaoyang.service.VoiceService
import com.zhouzi.zhaoyang.util.Calculator
import kotlinx.android.synthetic.main.transaction_query_detail_activity.*


class TransactionQueryDetailActivity : BaseActivity() ,ITransationQueryDetailActivityView{
    override fun errorToken() {
        exitToLogin()
    }
    var p = TransactionQueryDetailActivityPresenter()
    init {
        p.attachView(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        p.detachView()
    }

    override fun refundSuccess() {
        val intent3 = Intent(mContext, VoiceService::class.java)
        intent3.putExtra("voiceStr", "退款成功")
        startService(intent3)

        p.getOrder(outTradeNo)

        val activity = Intent(this, MainActivity::class.java)
        startActivity(activity)
        finish()

    }

    override fun refundFailed(msg: String) {
        val intent4 = Intent(mContext, VoiceService::class.java)
        intent4.putExtra("voiceStr", msg)
        startService(intent4)
    }


    override fun getOrderSuccess(orderDetail: OrderDetailBean?) {

        orderDetail.let {
            App.instance().printOrderWithReturnMoney(orderDetail!!)
        }

    }

    override fun getOrderFailed(msg: String?) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()
    }

    override fun showLoadDialog() {
        loadDialog.show()
    }

    override fun dismissLoadDialog() {
        loadDialog.dismiss()

    }

    override fun getLayoutId(): Int {
       return R.layout.transaction_query_detail_activity
    }

    override fun initView() {

        getIntentData()
        viewClick()
    }

    var outTradeNo:String = ""
    private fun getIntentData() {
        val data = intent.getParcelableExtra<GetOrderListBean2.RecordsBean>("data")
        outTradeNo = data.outTradeNo

        tvMoney.text = "￥ " +Calculator.fen2Yuan(data.orderTotalFee.toLong())
        tvRealMoney.text = "￥ " +Calculator.fen2Yuan(data.settlementTotalFee.toLong())
        tvVipMoney.text = "￥ " +Calculator.fen2Yuan(data.realPayFee.toLong())
        tvBackMoney.text ="￥ " + Calculator.fen2Yuan(data.couponFee.toLong())
        tvOrderCreateUserName.text = data.orderCreateUserName

        tvTradeNo.text = data.transactionId
        tvCommercialName.text = data.merchantName
        tvStoreName.text = data.storeName
        tvMerchantsNo.text = data.outTradeNo
        tvDeviceNo.text = Build.SERIAL
        tvOrderType.text = data.tradeType

        if(! TextUtils.isEmpty(data.orderRefundUserName) &&data.orderRefundUserName.equals("null")){
            tvRefundUserName.text = ""
        }else{
            tvRefundUserName.text =  data.orderRefundUserName

        }

        if (data.payType.equals("1") || data.payType == 1){
            tvPayMethods.text = "微信"
        }else if (data.payType.equals("2")|| data.payType == 2){
            tvPayMethods.text = "支付宝"

        }else if (data.payType.equals("3")|| data.payType == 3){
            tvPayMethods.text = "会员支付"

        }else if (data.payType.equals("4")|| data.payType == 4){
            tvPayMethods.text = "通用通"

        }

        if(data.orderStatus.equals("1")|| data.orderStatus == 1){
            tvPayStatus.text = "已付款"
        }else if (data.orderStatus.equals("3")|| data.orderStatus == 3){
            tvPayStatus.text = "退款"
        }




        tvCreateDate.text = data.updatedTime

    }

    private fun viewClick() {

        tvPrint.setOnClickListener {
            p.getOrder(outTradeNo)
        }

        tvBackCash.setOnClickListener {
            MaterialDialog.Builder(mContext)
                .title("确认退款？")
                .positiveText("确定")
                .negativeText("取消")
                .inputType(InputType.TYPE_TEXT_VARIATION_PASSWORD)
                .input("请输入管理员密码","", MaterialDialog.InputCallback { dialog, input ->

                })
                .onPositive { dialog, which ->
                    backCash(dialog.inputEditText!!.text.toString())
                }
                .onNeutral { dialog, which ->
                    dialog.dismiss()
                }
                .build().show()
        }

        tvApBtn.setOnClickListener {
            finish()
        }


    }

    private fun backCash(refundSecret : String) {
        Log.e("backCash",outTradeNo+refundSecret)
        var map = mutableMapOf(
            Pair("outTradeNo", outTradeNo),
            Pair("refundSecret",refundSecret )

        )
        p.refund(map)
    }


}
